extends Node

var game_item_list = null

# class member variables go here, for example:
# var a = 2
# var b = "textvar"

func _ready():
	game_item_list = init_game_item_list()

	
#FIXME: name
func find_game_item_data_by_id(id):
	for item in game_item_list:
		if not item.has("id"):
			print ("Warning: item without id found")
			continue
		
		if item["id"] == str(id):
			return item
	
	print ("item with id " + str(id) + " not found ??")
	return
		#pass

#FIXME: name
func get_game_item_value(item_data, key):
	if not item_data.has(key):
		print ("Warning: item_data has no key '" + key + "'")
		return
	else:
		return item_data[key]
	
	pass


func get_item_value(id, key):
	var item_data = find_game_item_data_by_id(id)
	
	if (item_data == null):
		return
	
	return get_game_item_value(item_data, key)


func init_game_item_list():
	
	var item_list_file = File.new()
	var path = "res://items.json"
	if not item_list_file.file_exists(path):
		print ("res://items.json -  path not found")
		return
		
	item_list_file.open(path, File.READ)
	var result_json = JSON.parse(item_list_file.get_as_text())
	
	if result_json.error == OK:  # If parse OK
		var data = result_json.result
		#print(data)
		return data.values()[0]
	else:  # If parse has errors
		print("Error: ", result_json.error)
		print("Error Line: ", result_json.error_line)
		print("Error String: ", result_json.error_string)
		return
